package com.podmanager.data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DateConverter {
	private Calendar myDate;
	
	static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss -SSSS";
	static final String DATE_FORMAT_2 = "EEE, dd MMM yyyy HH:mm:ss zzz";
	
	public DateConverter() { }
	
	public String ConvertDate(String inputDate) {
		String convertedDate = null;
		Date date = null;
		try {
			 date = new SimpleDateFormat(DATE_FORMAT).parse(inputDate);
			myDate = new GregorianCalendar();
			myDate.setTime(date);
			
			// allows date with single digit to have leading zero
			// for example instead of 2006-3-1, 2006-03-01 is the format
			convertedDate = String.format("%1$tY-%1$tm-%1$td", myDate);
		}
		catch (ParseException p) {
			try {
				date = new SimpleDateFormat(DATE_FORMAT_2).parse(inputDate);
				myDate = new GregorianCalendar();
				myDate.setTime(date);
				convertedDate = String.format("%1$tY-%1$tm-%1$td", myDate);
			} catch (ParseException e) {
				convertedDate = inputDate;
			}

		}
		catch (Exception e) {
			//e.printStackTrace();
			convertedDate = inputDate;
		}
		
		return convertedDate;
	}
}
